package eu.tolu.cableSpotter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import eu.tolu.cableSpotter.help.AppPreferences;
import eu.tolu.cableSpotter.help.BuildCableArray;
import eu.tolu.cableSpotter.R;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CountryList extends ListActivity {

	private ArrayList<String> countryAdapterArray = new ArrayList<String>();
	private ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.country_list);
		new RetreiveFeedTask2(this).execute();
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, countryAdapterArray);
		setListAdapter(adapter);
	}
    
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);

		Intent intent = new Intent(this, SpotList.class);

		for (String cabData : countryAdapterArray) {
			if (cabData.equals(l.getItemAtPosition(position).toString())) {
				intent.putExtra("country", l.getItemAtPosition(position)
						.toString());
			}
		}

		startActivity(intent);
	}

	public void setCountryArray(ArrayList<String> cabArr)
			throws IOException {
		System.out.println("SET COUNTRY ARRAY :"+cabArr.size());
		if(cabArr.size()<1){
			DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					switch (which) {
					case DialogInterface.BUTTON_POSITIVE:
						// Yes button clicked
						startActivity(new Intent(getApplicationContext(), DataOptions.class));
						break;

					case DialogInterface.BUTTON_NEGATIVE:
						// No button clicked
						break;
					}
				}
			};
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("No cables found, go to data options?")
					.setPositiveButton("Yes", dialogClickListener)
					.setNegativeButton("No", dialogClickListener).show();
		}
		countryAdapterArray.clear();
		for(String cab : cabArr){
			countryAdapterArray.add(cab);
			adapter.notifyDataSetChanged();
		}
	}
}

// -----------------------------------------------

class RetreiveFeedTask2 extends AsyncTask<String, Void, String> {

	private CountryList mainContext = null;
	private ArrayList<String> countryArray = new ArrayList<String>();

	public RetreiveFeedTask2(CountryList context) {
		mainContext = context;
		Log.v("LongOper", "Konstruktor");
	}

	@Override
	protected String doInBackground(String... params) {
		AppPreferences appPreferences = new AppPreferences(mainContext);
		countryArray = appPreferences.getCountryArray();
		return "test";
	}

	@Override
	protected void onPostExecute(String result) {
		Log.v("LongOper", "Set Array");
		try {
			mainContext.setCountryArray(countryArray);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
